package aliyun.cx.admin.mapper;

import aliyun.cx.admin.entity.ArticleTag;
import aliyun.cx.admin.entity.Tag;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Delete;
import java.util.List;

@Mapper
public interface ArticleTagMapper extends BaseMapper<ArticleTag> {
    
    /**
     * 根据文章ID获取标签列表
     */
    @Select("SELECT t.* FROM tag t " +
            "INNER JOIN article_tag at ON t.id = at.tag_id " +
            "WHERE at.article_id = #{articleId} AND t.deleted = 0")
    List<Tag> selectTagsByArticleId(Long articleId);
    
    /**
     * 根据标签ID获取文章ID列表
     */
    @Select("SELECT article_id FROM article_tag WHERE tag_id = #{tagId}")
    List<Long> selectArticleIdsByTagId(Long tagId);
    
    /**
     * 删除文章的所有标签关联
     */
    @Delete("DELETE FROM article_tag WHERE article_id = #{articleId}")
    int deleteByArticleId(Long articleId);
    
    /**
     * 删除标签的所有文章关联
     */
    @Delete("DELETE FROM article_tag WHERE tag_id = #{tagId}")
    int deleteByTagId(Long tagId);
}